import 'package:flutter/material.dart';

class DashedBox extends StatelessWidget {
  final double width;
  final double height;
  final int dashLength;
  final int dashGap;
  final Color dashColor;
  final Widget? child;

  DashedBox(
      {required this.width,
      required this.height,
      required this.dashLength,
      required this.dashGap,
      required this.dashColor,
      this.child});

  @override
  Widget build(BuildContext context) {
    return Container(
        width: width,
        height: height,
        child: Stack(
          children: [
            CustomPaint(
              painter: DashedBorderPainter(
                dashLength: dashLength,
                dashGap: dashGap,
                dashColor: dashColor,
                rect: Rect.fromLTWH(0, 0, width, height),
              ),
            ),
            child!
          ],
        ));
  }
}

class DashedBorderPainter extends CustomPainter {
  final int dashLength;
  final int dashGap;
  final Color dashColor;
  final Rect rect;

  DashedBorderPainter({
    required this.dashLength,
    required this.dashGap,
    required this.dashColor,
    required this.rect,
  });

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = dashColor
      ..strokeCap = StrokeCap.butt
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0;

    Path path = Path();

    // 计算每个虚线段和空隙的总长度
    double dashTotalLength = (dashLength + dashGap).toDouble();

    // 绘制顶部边界
    drawDashedLine(canvas, paint, dashTotalLength, rect.left, rect.top,
        rect.right, rect.top);

    // 绘制右侧边界
    drawDashedLine(canvas, paint, dashTotalLength, rect.right, rect.top,
        rect.right, rect.bottom);

    // 绘制底部边界
    drawDashedLine(canvas, paint, dashTotalLength, rect.left, rect.bottom,
        rect.right, rect.bottom);

    // 绘制左侧边界
    drawDashedLine(canvas, paint, dashTotalLength, rect.left, rect.top,
        rect.left, rect.bottom);

    // 重复上述逻辑，以完成其他三个边的绘制
    // 注意：实际实现中需要根据视图的实际尺寸来调整路径绘制的位置
  }

  void drawDashedLine(Canvas canvas, Paint paint, double dashTotalLength,
      double startX, double startY, double endX, double endY) {
    double dashCount =
        (endX - startX > endY - startY ? endX - startX : endY - startY) /
            dashTotalLength;

    for (int i = 0; i < dashCount; i++) {
      double dashStartX = startX + i * dashTotalLength;
      double dashStartY = startY + i * dashTotalLength;
      double dashEndX = dashStartX +
          (endX - startX == 0
              ? 0
              : (dashLength * (endX - startX).abs() / (endX - startX).abs()));
      double dashEndY = dashStartY +
          (endY - startY == 0
              ? 0
              : (dashLength * (endY - startY).abs() / (endY - startY).abs()));

      Path path = Path();
      path.moveTo(dashStartX, dashStartY);
      path.lineTo(dashEndX, dashEndY);
      canvas.drawPath(path, paint);
    }
  }

  @override
  bool shouldRepaint(DashedBorderPainter oldDelegate) {
    return false;
  }
}
